@extends('admin.layouts.app')
@section('css')
	<link href="{{asset('assets/plugins/select2/css/select2.min.css')}}" rel="stylesheet" type="text/css" />
 <link href="{{asset('assets/plugins/bootstrap-daterangepicker/daterangepicker.css')}}" rel="stylesheet">
@endsection
@section('content')


						<!-- Page-Title -->
						<div class="row">
							<div class="col-sm-12">
                                

								<h4 class="page-title">管理后台</h4>
								<ol class="breadcrumb">
									<li>
										<a href="{{url('zadmin/')}}">塔台</a>
									</li>
									<li class="active">
										<a >订单管理</a>
									</li>
									
								</ol>
							</div>
						</div>
						

					
	<div class="row">                      				
        	
			
		<div class="card-box">
		<form class="form-inline">
		
        	<div class="form-group input-group col-lg-12">
        			
                <input type="text"  name="keywords" class="form-control" placeholder="可以根据订单编号，买家手机号，姓名,商户名搜索" required="" value="{{array_get($where,'keywords')}}">
                <span class="input-group-btn">
                    <button type="submit" class="btn waves-effect waves-light btn-primary">搜索</button>
                </span>
            </div>
        </form>
         
			
			

			
		<form class="form-inline">	

			<div class="form-group m-r-10 m-t-15">
			<label class="m-r-10">订单状态</label>
			<select class="form-control" name="status">
				<option value="">全部</option>
				@foreach($status_arr as $k=>$val)
				<option value="{{$k}}">{{$val}}</option>
				@endforeach
				
				
			</select>
			</div>

			<div class="form-group m-r-10 m-t-15">
			<label class="m-r-10">商品分类</label>
			<select class="form-control" name="product_cate_id">
				<option value="">请选择</option>
				@foreach($cates as $cate)
				<option value="{{$cate->id}}" {{array_get($where,'product_cate_id')==$cate->id?'selected':''}}>{{$cate->name}}</option>
				@endforeach
				
			</select>
			</div>

			<div class="form-group m-r-10 m-t-15">
			<label class="m-r-10">订单类型</label>
			<select class="form-control" name="product_type">
				<option value="">请选择</option>
				
				<option value="1" {{array_get($where,'product_type')==1?'selected':''}}>快递类</option>
				<option value="2" {{array_get($where,'product_type')==2?'selected':''}}>核销类</option>
				
			</select>
			</div>
			
			<div class="form-group m-r-10 m-t-15">
                <label class="control-label m-r-10 m-t-10">日期</label>
               
                    <div id="reportrange" class="pull-right form-control">
                        <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
                        <span></span>
                	</div>
                
            </div>
            <div class="form-group m-t-15">
				<button type="submit" class="btn btn-default waves-effect waves-light btn-md">
					搜索
				</button>
			</div>
			</form>
        </div>    
        
		
	
	
					
    
@include('admin.common.error')
                          
	<div class="col-lg-12">
								
	@foreach($list as $v) 
        <div class="portlet">
            <div class="portlet-heading bg-{{$v->status_style}}" style="padding: 5px 10px;">
                <span>订单编号：{{$v->code}} </span>
                <div class="portlet-widgets">
                	
                	<span> 创建时间：{{$v->created_at}}</span>
                	

                </div>
                                      
                <div class="clearfix"></div>
            </div>
                <div  class="panel-collapse collapse in">
                    <div class="portlet-body" style="padding: 5px;">
                        <div class="table-responsive" >
				<table  class="table">

					<tbody>
						
                    @foreach($v->products as $op)   
						<tr>
							<td><img src="{{$op->product_thumb}}" width="60" class="img-rounded" /></td>
							<td>{{$op->product_name}} <label class="text-default">{{$op->product_spec}}</label></td>			
								
							<td>￥ {{$op->product_price}} X{{$op->product_num}}</td>
							<td colspan="2">￥ {{$op->subtotal}}</td>
							
							

						</tr>
						
					@endforeach
					<tr>
							<td>共{{$v->products->count()}}件商品, 合计：￥{{$v->total_price_txt}}元
							</td>
							<td>
							下单用户：{{object_get($v,'user.nickname')}}
							</td>
							<td>
								@if($v->address)
								<span class="text-purple">
								收货人：{{$v->address}}， {{$v->truename}}，{{$v->mobile}}
								</span>
								@endif
							</td>
							<td>
								<span class="label label-{{$v->status_style}}"> {{$v->shop_status}}
								</span>
							</td>
							<td>
							@if(in_array($v->status,['1','2']))	
							<a href="{{url('zadmin/order/reject')}}?order_id={{$v->id}}" class="btn btn-sm btn-warning">取消</a>
							
							<a href="{{url('zadmin/order/confirm')}}?order_id={{$v->id}}" class="btn btn-sm btn-primary">确认</a>
							@endif
							@if($v->transaction_id && $v->status!='-100')
							<button type="button" class="btn btn-danger btnRefund btn-sm" data-code="{{$v->code}}" data-totalprice="{{$v->total_price}}"
							data-id="{{$v->id}}" >
								退款
							</button>
							@endif

							@if($v->status>3) 
							<a href="{{url('zadmin/order/complete')}}?order_id={{$v->id}}" class="btn btn-sm btn-success">{{$v->shop_status}}</a>
							@endif
							</td>	
							
						</tr>
						@if($v->dispatch_code)
						<tr class="text-primary">
							<td>快递公司: {{$v->dispatch_company}}</td>
							<td>快递单号: {{$v->dispatch_code}}</td>
							<td>发货时间: {{$v->dispatched_at}}</td>
						</tr>
						@endif
						@if($v->status==3)
						
						<tr>
							<td colspan="4">
			<form class="form-inline" action="{{url('zadmin/order/send')}}" method="post">				
			<div class="row">
	        	<div class="form-group col-lg-4">
	        			
	                <input type="text"  name="dispatch_company" class="form-control" placeholder="快递公司名" required="">
	                
	            </div>
	            <div class="form-group col-lg-4">
	        			
	                <input type="text"  name="dispatch_code" class="form-control" placeholder="快递单号" required="">
	                
	            </div>	
	            <div class="form-group col-lg-4">
	            	<button type="submit"  class="btn btn-sm btn-primary">发货</a>
	            </div>
        	</div>
        	{{csrf_field()}}
        	<input name="id" value="{{$v->id}}" type="hidden"  />
        	</form>	
							</td>
								
						</tr>
						
						@endif
                    </tbody>
				</table>
										

									</div>
                                        </div>
                                    </div>
                                </div>
                            						
		 						@endforeach
			

								
							</div>
							 
							  {{$list->appends($where)->links()}}
						</div>
						<!-- end row -->
 @endsection 
 @section('modal')
 <div class="modal fade" id="refundModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">订单退款</h4>
            </div>
            <div class="modal-body">
            	<form>
            		<div class="form-group">
                        <label class="control-label">订单号</label>
                       
                            <input type="text" class="form-control" name="orderCode" >
                        
                    </div>
                    <div class="form-group">
                        <label class="control-label">退款金额</label>
                       
                            <input type="text" class="form-control" name="orderTotalPrice"  />
                        
                    </div>
            		<div class="form-group">
                        <label class="control-label">备注</label>
                       
                        <textarea class="form-control" name="remark" id="remark"></textarea>
                        
                    </div>
            	</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="refundSubmit">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
 @endsection
               
         

    @section('js')
    <script src="{{asset('assets/plugins/select2/js/select2.min.js')}}" type="text/javascript"></script>
    <script src="{{asset('assets/plugins/moment/moment.js')}}"></script>    
    <script src="{{asset('assets/plugins/bootstrap-daterangepicker/daterangepicker.js')}}"></script>
    <script type="text/javascript">
    	var order_id;
    	$('#reportrange span').html(moment().subtract(29, 'days').format('YYYY-MM-DD') + ' 至 ' + moment().format('YYYY-MM-DD'));
		
		        $('#reportrange').daterangepicker({
		            format: 'YYYY-MM-DD',
		            startDate: moment().subtract(29, 'days'),
		            endDate: moment(),
		            minDate: '01-01-2018',
		            maxDate: '12-31-2099',
		            dateLimit: {
		                days: 60
		            },
		            showDropdowns: true,
		            showWeekNumbers: true,
		            timePicker: false,
		            timePickerIncrement: 1,
		            timePicker12Hour: true,
		            ranges: {
		                '今天': [moment(), moment()],
		                '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
		                '最近七天': [moment().subtract(6, 'days'), moment()],
		                '最近30天': [moment().subtract(29, 'days'), moment()],
		                '本月': [moment().startOf('month'), moment().endOf('month')],
		                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
		            },
		            opens: 'left',
		            drops: 'down',
		            buttonClasses: ['btn', 'btn-sm'],
		            applyClass: 'btn-default',
		            cancelClass: 'btn-white',
		            separator: ' to ',
		            locale: {
		                applyLabel: '确定',
		                cancelLabel: '取消',
		                fromLabel: 'From',
		                toLabel: 'To',
		                customRangeLabel: '自定义',
		                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
		                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
		                firstDay: 1
		            }
		        }, function (start, end, label) {
		            console.log(start.toISOString(), end.toISOString(), label);
		            $('#reportrange span').html(start.format('YYYY-MM-DD') + ' 至 ' + end.format('YYYY-MM-DD'));
		});

	// 商品
	$('#shop_id').on('change',function(){
		$('#product_id').empty();
		var shop_id = $(this).val();
		var url = "{{url('api/products')}}";
		$.get(url,{'shop_id':shop_id},function(rs){
			if(rs.status) {				
				var $options = '';
				$.each(rs.data,function(k,v){
					$options +='<option value="'+v.id+'">'+v.name+'</option>';
				});	
				$('#product_id').append($options);
			} else {
				alert('获取数据失败');
			}
		});
	})	

	$('.btnRefund').on('click',function(){
		$('#refundModal').modal('show');
		var _this      = $(this);
		var totalPrice = _this.data('totalprice')/100;
		var code       = _this.data('code');
		order_id = _this.data('id');
		

		$('input[name="orderCode"]').val(code).attr('readonly','true');
		$('input[name="orderTotalPrice"]').val(totalPrice).attr('readonly','true');
		


	}) 

	$('#refundSubmit').on('click',function(){
		var data  = {};
		data.id 	= order_id;
		data.remark = $('#remark').val();
		data._token = "{{csrf_token()}}";
		var url = "{{url('zadmin/refunds')}}";
		$.post(url,data,function(rs){
			if(rs.status==true) {
				location.reload(true);
			}
		})
	}) ;      
    </script>
    @endsection    

